﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
using rock.UI;
using rock.UI.houseinfo;
using rock.UI.common;
using rock.DAL;
using rock.UI.softinfo;
using rock.common;
using rock.UI.message;
using System.IO;
using System.Drawing.Drawing2D;
using System.Threading;
using rock.UI.system;
 

namespace rock.UI
{
    public partial class Main : Form
    {
        public Login Login { get; set; }
       // Sunisoft.IrisSkin.SkinEngine skin;

       
        public Main()
        {
            InitializeComponent();
            this.Icon = global::rock.Properties.Resources.house;
        }
       
        private void Main_Load(object sender, EventArgs e)
        {
            
            //


             
           
            addTopButtopListener();

            initTaskBarInfo();


            //初使皮肤
            String skinFile = ConfigUtils.GetAppConfig("skin");
            if (skinFile != null && skinFile.EndsWith(".ssk"))
            {
                setSkin(skinFile);
            }
            else
            {
                setSkin("DiamondBlue.ssk");
            }


 


            if (Manager.getInstance().userAutho.MESSAGE_AUTHO == 1) {
                //pictureBox1_Click(this.labelMessage, null);
                showBodyPanel(MODEL_CONSTANT.model_message, 0);

            }else  if (Manager.getInstance().userAutho.FEE_AUTHO == 1) {
                //pictureBox1_Click(this.label14Fee, null);
                showBodyPanel(MODEL_CONSTANT.model_fee, 0);

            }else  if (Manager.getInstance().userAutho.REPORT_AUTHO == 1) {
                //pictureBox1_Click(this.label13Report, null);
                showBodyPanel(MODEL_CONSTANT.model_report, 0);

            }else if (Manager.getInstance().userAutho.HOUSE_AUTHO == 1) {
                //pictureBox1_Click(this.label12House, null);
                showBodyPanel(MODEL_CONSTANT.model_house, 0);

            }else if (Manager.getInstance().userAutho.BASE_AUTHO == 1) {
                pictureBox1_Click(this.label11Base, null);
                showBodyPanel(MODEL_CONSTANT.model_base, 0);

            }else if (Manager.getInstance().userAutho.CONFIG_AUTHO == 1) {
                //pictureBox1_Click(this.label15Config, null);
                showBodyPanel(MODEL_CONSTANT.model_config, 0);

            }
            else if (Manager.getInstance().userAutho.PICTURE_AUTHO == 1)
            {
               // pictureBox1_Click(this.label7Picture, null);
                showBodyPanel(MODEL_CONSTANT.model_picture, 0);

            }
            else {

                UserControlMainDefault userControl = new UserControlMainDefault();
                showUserControls2BodyPanel(userControl , 0);
            }

        }
        UserControMainPictureInfo userControl;
      
         private void  initsPictureView(){
             userControl = new  UserControMainPictureInfo();
            
        }
        

        

        private void initTaskBarInfo()
        {


            this.toolStripStatusLabel1Time.Text = Manager.getInstance().userAccount.USERNAME + "已登陆,时间:" + DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss") + ";";
            //this.toolStripStatusLabel2User.Text = "登陆用户:" +Manager.getInstance().userAccount.ACCOUNT+ "";


            DalHOUSE_ROOM dal = new DalHOUSE_ROOM();
            DataSet ds =  dal.GetList("");
            int unrent = 0;
            int rented = 0;
            int yuding = 0;
            int pasue = 0;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                int status = (int)ds.Tables[0].Rows[i]["STATUS"];


                switch (status)
                {
                    case Constant.HOUSE_ROOM_STATUS_UNRENT:
                        unrent++;
                        break;
                    case Constant.HOUSE_ROOM_STATUS_RENTED:
                        rented++;
                        break;
                    case Constant.HOUSE_ROOM_STATUS_YUDING:
                        yuding++;
                         break;
                    case Constant.HOUSE_ROOM_STATUS_PAUSE:
                         pasue++;
                         break;

                    default:
                        break;
                }

            }
            String rentInfo = "房屋出租情况:总房数{0},未出租{1},已出租{2}, 暂停出租{3}";
            rentInfo = String.Format(rentInfo ,ds.Tables[0].Rows.Count,  unrent, rented, pasue);
          //  this.mainInfo.Text = rentInfo;
           this.toolStripStatusLabel3RentInfo.Text = rentInfo;
           String sqlCon = ConfigUtils.GetConnectionStringsConfig("rock.Properties.Settings.RentalHousingManagerConnectionString");
           this.toolStripStatusLabel4Dbinfo.Text = "数据库服务器:" + sqlCon.Split(new char[2] {';','='})[1];

        }

        

       
        //更换主面板的PANEL
        private void showUserControls2BodyPanel(UserControl _UserControl , int index )
        {
            
            //foreach (Control c in bodyPanel.Controls)
            //{
            //    bodyPanel.Controls.Remove(c);

            //}
             
            //bodyPanel.Controls.Add(_UserControl);

            Panel parentControl = bodyPanel;//你的容器控件   
            while (parentControl.Controls.Count > 0)
            
            {
                if (parentControl.Controls[0] != null)
                    parentControl.Controls[0].Dispose();
            }
            ((UserControlMainSuper)_UserControl).selectIndex(index);
            bodyPanel.Controls.Add(_UserControl);


        }


      
 
       

         
        
        
        
        
         

        List<Label> panels = new List<Label>();
            
        public void addTopButtopListener() {
 
          
            
           
 
            panels.Add(this.label11Base);
            panels.Add(this.label12House);
            panels.Add(this.label13Report);
            panels.Add(this.label14Fee);
            panels.Add(this.label15Config);
            panels.Add(this.labelMessage);
            panels.Add(this.label10ExitSys);
            panels.Add(this.label7ChgPwd);
            panels.Add(this.label7Picture);
            panels.Add(this.label10Lock);

            for (int i = 0; i < panels.Count; i++)
			{
                Label picturebox = panels[i];
                picturebox.Click += new System.EventHandler(this.pictureBox1_Click);
                picturebox.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
               
                picturebox.MouseEnter += new System.EventHandler(this.pictureBox1_MouseEnter);
                picturebox.MouseLeave += new System.EventHandler(this.pictureBox1_MouseLeave);
                //label_Paint
                //picturebox.Paint += label_Paint;
			} 

             
        }
         
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            // 正 WhiteSmoke  选 LightGray  按 Bisque
            ((Label)sender).BackColor = System.Drawing.Color.Bisque;  
        }
        

        private void pictureBox1_MouseEnter(object sender, EventArgs e)
        {

            for (int i = 0; i < panels.Count; i++)
            {

                if (panels[i].Equals(sender))
                {
                    // 正 WhiteSmoke  选 LightGray  按 Bisque
                    this.panels[i].BackColor = System.Drawing.Color.LightGray;  
                }else
                    if (clickPanel != null && panels[i].Equals(clickPanel))
                {
                    // 正 WhiteSmoke  选 LightGray  按 Bisque
                    this.panels[i].BackColor = System.Drawing.Color.Bisque; 
                }
                else
                {
                  //  panels[i].BorderStyle = BorderStyle.None;
                    this.panels[i].BackColor = System.Drawing.Color.WhiteSmoke;  // WhiteSmoke  LightGray
                }

            }
            
             
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {

            for (int i = 0; i < panels.Count; i++)
            {

                if (clickPanel != null && panels[i].Equals(clickPanel))
                {
                    //clickPanel.BorderStyle = BorderStyle.FixedSingle;
                    this.panels[i].BackColor = System.Drawing.Color.Bisque;  // WhiteSmoke  LightGray

                }
                else
                {
                   // panels[i].BorderStyle = BorderStyle.None;
                    this.panels[i].BackColor = System.Drawing.Color.WhiteSmoke;  // WhiteSmoke  LightGray
                }
                
            }
           
           
        }
        //private void label_Paint(object sender, PaintEventArgs e)
        //{
        //   // DrawRoundRect(e.Graphics, (Label)sender);
        //}
        private void DrawRoundRect(Graphics graphics, Label label)
        {
            float X = float.Parse(label.Width.ToString()) - 1;
            float Y = float.Parse(label.Height.ToString()) - 1;
            PointF[] points = {
        new PointF(2,     0),
        new PointF(X-2,   0),
        new PointF(X-1,   1),
        new PointF(X,     2),
        new PointF(X,     Y-2),
        new PointF(X-1,   Y-1),
        new PointF(X-2,   Y),
        new PointF(2,     Y),
        new PointF(1,     Y-1),
        new PointF(0,     Y-2),
        new PointF(0,     2),
        new PointF(1,     1)
        };
            GraphicsPath path = new GraphicsPath();
            path.AddLines(points);
            Pen pen = new Pen(Color.FromArgb(150, Color.Blue), 1);
            pen.DashStyle = DashStyle.Solid;
            graphics.DrawPath(pen, path);
        }


   

        Label clickPanel = null;
        private void pictureBox1_Click(object sender, EventArgs e)
        {

            
            if ( sender.Equals(this.labelMessage)) 
            {
                showBodyPanel(MODEL_CONSTANT.model_message, 0);
            }
            else if (  sender.Equals(this.label14Fee)) 
            {
                showBodyPanel(MODEL_CONSTANT.model_fee, 0);
            }
            else if (  sender.Equals(this.label13Report)) 
            {
                showBodyPanel(MODEL_CONSTANT.model_report, 0);
            }
            else if (  sender.Equals(this.label12House)) 
            {
                showBodyPanel(MODEL_CONSTANT.model_house, 0);

            }
            else if (  sender.Equals(this.label11Base)) 
            {
                showBodyPanel(MODEL_CONSTANT.model_base, 0);
               
            }
            else if (  sender.Equals(this.label15Config)) 
            {
                showBodyPanel(MODEL_CONSTANT.model_config, 0);
            }
            else if(sender.Equals(this.label7Picture))
            {
                showBodyPanel(MODEL_CONSTANT.model_picture, 0);
            }
            else if (sender.Equals(this.label10ExitSys))
            {

                DialogResult dialogResult = MessageBox.Show("请确认要退出系统！" , "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dialogResult.Equals(DialogResult.OK))
                {
                    Exit();
                }
            }
            else if (sender.Equals(this.label7ChgPwd))
            {
                FormChangePwd pwd = new FormChangePwd();
                pwd.ShowDialog(this);
            }
            else if (sender.Equals(this.label10Lock))
            { 
                toolStripMenuItem2_Click_1(null, null);
            }

        }


        public void setClick(String model ) {
            if (model.Equals(MODEL_CONSTANT.model_base)) {
                clickPanel = this.label11Base; 
            }
            else if (model.Equals(MODEL_CONSTANT.model_message))
            {
                 clickPanel = this.labelMessage ; 
            }
            else if (model.Equals(MODEL_CONSTANT.model_fee))
            {
                 clickPanel = this.label14Fee ; 
            }
            else if (model.Equals(MODEL_CONSTANT.model_house))
            {
                 clickPanel = this.label12House ; 
            }
            else if (model.Equals(MODEL_CONSTANT.model_report))
            {
                 clickPanel = this.label13Report ; 
            }
            else if (model.Equals(MODEL_CONSTANT.model_config))
            {
                 clickPanel = this.label15Config ; 
            }
            else if (model.Equals(MODEL_CONSTANT.model_picture))
            {
                 clickPanel = this.label7Picture ; 
            }
            else if (model.Equals(MODEL_CONSTANT.model_fee))
            {
                clickPanel = this.label14Fee ; 
            }

             



            for (int i = 0; i < panels.Count; i++)
            {
                if (clickPanel != null && panels[i].Equals(clickPanel))
                {
                    //clickPanel.BorderStyle = BorderStyle.FixedSingle;
                    // 正 WhiteSmoke  选 LightGray  按 Bisque
                    this.panels[i].BackColor = System.Drawing.Color.Bisque;
                }
                else
                {
                    //panels[i].BorderStyle = BorderStyle.None;
                    // 正 WhiteSmoke  选 LightGray  按 Bisque
                    this.panels[i].BackColor = System.Drawing.Color.WhiteSmoke;
                }

            }
        }



      
        
        

        private void REDToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setSkin("DiamondRed.ssk");
        }

        private void BULEToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setSkin("DiamondBlue.ssk");
        }

        private void ZHISHEToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setSkin("DiamondPurple.ssk");
        }

        private void HESHEToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setSkin("DiamondOlive.ssk");
        }

        private void YELLOWToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setSkin("DiamondGreen.ssk");
        }

        private void DEFAULTToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setSkin("Defalult.ssk");
        }

        private void setSkin(String skinFile) {

            if (skinFile.Equals("Defalult.ssk"))
            {
                Manager.getInstance().skinEngine.SkinFile = null;
                this.HeadPanelParent.BackgroundImage = global::rock.Properties.Resources.top_bg_blue;
                this.HeadPanelParent.BackgroundImageLayout = ImageLayout.Stretch;
                Manager.getInstance().skinEngine.Active = false;
                ConfigUtils.UpdateAppConfig("skin", skinFile);
                return;
            }
            else if (skinFile.Equals("DiamondGreen.ssk"))
            {
                Manager.getInstance().skinEngine.SkinStream = new MemoryStream(global::rock.Properties.Resources.DiamondGreen);
                this.HeadPanelParent.BackgroundImage = global::rock.Properties.Resources.top_bg_green;
            }
            else if (skinFile.Equals("DiamondOlive.ssk"))
            {
                Manager.getInstance().skinEngine.SkinStream = new MemoryStream(global::rock.Properties.Resources.DiamondOlive);
                this.HeadPanelParent.BackgroundImage = global::rock.Properties.Resources.top_bg_he;
            }
            else if (skinFile.Equals("DiamondPurple.ssk"))
            {
                Manager.getInstance().skinEngine.SkinStream = new MemoryStream(global::rock.Properties.Resources.DiamondPurple);
                this.HeadPanelParent.BackgroundImage = global::rock.Properties.Resources.top_bg_zhi;
            }
            else if (skinFile.Equals("DiamondBlue.ssk"))
            {
                Manager.getInstance().skinEngine.SkinStream = new MemoryStream(global::rock.Properties.Resources.DiamondBlue);
                this.HeadPanelParent.BackgroundImage = global::rock.Properties.Resources.top_bg_blue2;
            }
            else if (skinFile.Equals("DiamondRed.ssk"))
            {
                Manager.getInstance().skinEngine.SkinStream = new MemoryStream(global::rock.Properties.Resources.DiamondRed);
                this.HeadPanelParent.BackgroundImage = global::rock.Properties.Resources.top_bg_red2;
            }


            this.HeadPanelParent.BackgroundImageLayout = ImageLayout.Stretch;
            Manager.getInstance().skinEngine.Active = true;
            ConfigUtils.UpdateAppConfig("skin", skinFile);
        
        }


        private void Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dialogResult = MessageBox.Show("请确认要退出系统！", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

            if (dialogResult.Equals(DialogResult.OK))
            {
                Exit();
            }
            else 
            {

                e.Cancel = true; 
            }
        }

        public void Exit() {
            if (Login != null)
            {
                Login.Close();
            }
            System.Environment.Exit(System.Environment.ExitCode);
        }

        


        public void showBodyPanel(String message , int index ) {

            setClick(message);

            UserControlMainSuper userControlMain = null;

            if(message.Equals(MODEL_CONSTANT.model_fee)){
                if (Manager.getInstance().userAutho.FEE_AUTHO != 1)
                {
                    MessageBox.Show(this, "对不起,你没有此模块的权限.", "提示");
                    return;
                }
                if (this.bodyPanel.Controls.Count == 1)
                {
                    Control control = this.bodyPanel.Controls[0];
                    if (control.GetType() == typeof(UserControlMainFee))
                    {
                        ((UserControlMainSuper)control).selectIndex(index);
                        return;
                    }
                }
                userControlMain = new UserControlMainFee();
            
            } else

            if(message.Equals(MODEL_CONSTANT.model_base)){
                

                if (Manager.getInstance().userAutho.BASE_AUTHO != 1)
                {
                    MessageBox.Show(this, "对不起,你没有此模块的权限.", "提示");
                    return;
                }
                if (this.bodyPanel.Controls.Count == 1)
                {
                    Control control = this.bodyPanel.Controls[0];
                    if (control.GetType() == typeof(UserControlMainBaseInfo))
                    {
                        ((UserControlMainSuper)control).selectIndex(index);
                        return;
                    }
                }

                userControlMain = new UserControlMainBaseInfo();
            } else
            if (message.Equals(MODEL_CONSTANT.model_report))
            {
                if (Manager.getInstance().userAutho.REPORT_AUTHO != 1)
                {
                    MessageBox.Show(this, "对不起,你没有此模块的权限.", "提示");
                    return;
                }
                if (this.bodyPanel.Controls.Count == 1)
                {
                    Control control = this.bodyPanel.Controls[0];
                    if (control.GetType() == typeof(UserControlMainReport))
                    {
                        ((UserControlMainSuper)control).selectIndex(index);
                        return;
                    }
                }
                userControlMain = new UserControlMainReport();
            }
            else
            if (message.Equals(MODEL_CONSTANT.model_house))
            {
                if (Manager.getInstance().userAutho.HOUSE_AUTHO != 1)
                {
                    MessageBox.Show(this, "对不起,你没有此模块的权限.", "提示");
                    return;
                }
                if (this.bodyPanel.Controls.Count == 1)
                {
                    Control control = this.bodyPanel.Controls[0];
                    if (control.GetType() == typeof(UserControlMainHouseInfo))
                    {
                        ((UserControlMainSuper)control).selectIndex(index);
                        return;
                    }
                }
                userControlMain = new UserControlMainHouseInfo();
            }
            else
            if (message.Equals(MODEL_CONSTANT.model_config))
            {
                if (Manager.getInstance().userAutho.CONFIG_AUTHO != 1)
                {
                    MessageBox.Show(this, "对不起,你没有此模块的权限.", "提示");
                    return;
                }
                if (this.bodyPanel.Controls.Count == 1)
                {
                    Control control = this.bodyPanel.Controls[0];
                    if (control.GetType() == typeof(UserControlMainSystemconfig))
                    {
                        ((UserControlMainSuper)control).selectIndex(index);
                        return;
                    }
                }
                userControlMain = new UserControlMainSystemconfig();
            }
            else
            if (message.Equals(MODEL_CONSTANT.model_picture))
            {
                if (Manager.getInstance().userAutho.PICTURE_AUTHO != 1)
                {
                    MessageBox.Show(this, "对不起,你没有此模块的权限.", "提示");
                    return;
                }
                if (this.bodyPanel.Controls.Count == 1)
                {
                    Control control = this.bodyPanel.Controls[0];
                    if (control.GetType() == typeof(UserControMainPictureInfo))
                    {
                        ((UserControlMainSuper)control).selectIndex(index);
                        return;
                    }
                }
                userControlMain = new UserControMainPictureInfo();
            }
            else
            if (message.Equals(MODEL_CONSTANT.model_message))
            {
                if (Manager.getInstance().userAutho.MESSAGE_AUTHO != 1)
                {
                    MessageBox.Show(this, "对不起,你没有此模块的权限.", "提示");
                    return;
                }
                if (this.bodyPanel.Controls.Count == 1)
                {
                    Control control = this.bodyPanel.Controls[0];
                    if (control.GetType() == typeof(UserControlMainMessage))
                    {
                        ((UserControlMainSuper)control).selectIndex(index);
                        return;
                    }
                }
                userControlMain = new UserControlMainMessage();
            }
            else {
                MessageBox.Show("没有这个模块喽");
                return;

            }


            showUserControls2BodyPanel((UserControl)userControlMain, index);
        }

        private void ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String desc = ((ToolStripMenuItem)sender).AccessibleDescription;
            String name = ((ToolStripMenuItem)sender).AccessibleName;
            showBodyPanel(desc , Int32.Parse(name));
        }



        private void xgmmtoolStripMenuItem2_Click(object sender, EventArgs e)
        {
            FormChangePwd pwd = new FormChangePwd();
                pwd.ShowDialog(this);
        }

     
        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 ab = new AboutBox1();
            ab.ShowDialog(this);
        }


        private void gyxxToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 ab = new AboutBox1();
            ab.ShowDialog(this);
        }

        private void tcxttoolStripMenuItem3_Click(object sender, EventArgs e)
        {
             
            DialogResult dialogResult = MessageBox.Show("请确认要退出系统！" , "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dialogResult.Equals(DialogResult.OK))
            {
                Exit();
            }
             
        }

         
        private void xtbzToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FileUtils.openDox(AppDomain.CurrentDomain.BaseDirectory+"\\涟水军民恒宇市场租赁系统帮助文档.doc");
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            Form1Database fd = new Form1Database();
            fd.ShowDialog(this);
        }

        //锁定
        private void toolStripMenuItem2_Click_1(object sender, EventArgs e)
        {
            FormSystemLock fsl = new FormSystemLock();
            fsl.ShowDialog(this);
        }

    }
    
     
}
